package handel

import (
	"github.com/gin-gonic/gin"
	"repo-nav1/model"
	"strconv"
)

func ShopAdd(c *gin.Context) {
	shop_code := c.PostForm("shop_code")
	shop_name := c.PostForm("shop_name")
	shop_address := c.PostForm("shop_address")
	data := model.Repo{
		RepoCode:    shop_code,
		RepoName:    shop_name,
		RepoAddress: shop_address,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func ShopList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var rope []model.Repo
	model.DB.Offset(offset).Limit(size).Find(&rope)
	var total int64
	model.DB.Model(&model.Shop{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "展示成功",
		"data":  rope,
		"total": total,
	})
}
func RopeUpdate(c *gin.Context) {
	id := c.PostForm("id")
	repo_name := c.PostForm("repo_name")
	var repo model.Repo
	model.DB.Where("id=?", id).First(&repo).Update("repo_name", repo_name)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "修改成功",
	})
	return
}
